package org.idea.spring.aop.demo;

import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.idea.spring.aop.version2.bean.Advice;
import org.springframework.aop.Advisor;

import java.util.Arrays;

/**
 * @Author linhao
 * @Date created in 10:52 上午 2021/5/4
 */
@Aspect
public class LogAspect {

    @Pointcut("execution(* org.idea.spring.aop.demo.MyCalculate.*(..))")
    public void pointCut() {
    }

    @Before(value = "pointCut()")
    public void methodBefore(JoinPoint joinPoint) {
        Advisor advisor;
        System.out.println("执行目标方法before：" + joinPoint.getSignature().getName() + " ,参数为：" + Arrays.asList(joinPoint.getArgs()));
    }


    @After(value = "pointCut()")
    public void methodAfter(JoinPoint joinPoint) {
        System.out.println("执行目标方法after：" + joinPoint.getSignature().getName() + " ,参数为：" + Arrays.asList(joinPoint.getArgs()));
    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void methodAfterReturn(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("执行目标方法after return: " + methodName + ",after return is：" + result + " ,参数为：" + Arrays.asList(joinPoint.getArgs()));
    }

    @AfterThrowing(value = "pointCut()")
    public void methodAfterThrowing(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("执行目标方法after throwing: " + methodName + " ,参数为：" + Arrays.asList(joinPoint.getArgs()));
    }

    @Around(value = "pointCut()")
    public Object methodAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object result = proceedingJoinPoint.proceed();
        return result;
    }


}
